package com.app.studyroom.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.app.studyroom.R;
import com.app.studyroom.api.model.ssrSeat.SeatItem;
import com.app.studyroom.mapper.AreaNameToIdMapper;
import com.app.studyroom.mapper.SeatStatusMapper;

import java.util.List;
import java.util.Map;

public class SeatManageAdapter extends RecyclerView.Adapter<SeatManageAdapter.SeatViewHolder> {

    public interface OnSeatActionListener {
        void onEdit(SeatItem seat);
        void onDelete(SeatItem seat);
    }

    private List<SeatItem> seatList;
    private OnSeatActionListener listener;
    private final Map<String, String> seatBindIdToNameCache;
    public SeatManageAdapter(List<SeatItem> seatList, OnSeatActionListener listener, Map<String, String> seatBindIdToNameCache) {
        this.seatList = seatList;
        this.listener = listener;
        this.seatBindIdToNameCache = seatBindIdToNameCache;
    }


    public void setSeatList(List<SeatItem> seatList) {
        this.seatList = seatList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public SeatViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_seat_manage, parent, false);
        return new SeatViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SeatViewHolder holder, int position) {
        SeatItem seat = seatList.get(position);

        holder.tvSeatNum.setText(seat.getSeatNum());
        holder.tvSeatPlace.setText(AreaNameToIdMapper.getNameById(seat.getSeatPlace()));
        holder.tvStatus.setText(SeatStatusMapper.getNameByCode(seat.getSeatStatus()));
        String bindId = seat.getSeatBind();
        String displayName = bindId != null ? seatBindIdToNameCache.getOrDefault(bindId, bindId) : "无";
        holder.tvBindUser.setText(displayName);
        holder.btnEdit.setOnClickListener(v -> {
            if (listener != null) listener.onEdit(seat);
        });

        holder.btnDelete.setOnClickListener(v -> {
            if (listener != null) listener.onDelete(seat);
        });
        if (seat.getQyAdminMap() != null && seat.getQyAdminMap().getName() != null) {
            holder.tvQyAdmin.setText(seat.getQyAdminMap().getName());  // ✅ 显示管理员名字
        } else {
            holder.tvQyAdmin.setText("未指定");  // 或 fallback 到 ID：seat.getQyAdmin()
        }

    }

    @Override
    public int getItemCount() {
        return seatList != null ? seatList.size() : 0;
    }

    public static class SeatViewHolder extends RecyclerView.ViewHolder {
        TextView tvSeatNum, tvSeatPlace, tvStatus, tvQyAdmin, tvBindUser;
        Button btnEdit, btnDelete;

        public SeatViewHolder(@NonNull View itemView) {
            super(itemView);
            tvSeatNum = itemView.findViewById(R.id.tv_seat_num);
            tvSeatPlace = itemView.findViewById(R.id.tv_seat_place);
            tvStatus = itemView.findViewById(R.id.tv_status);
            tvQyAdmin=itemView.findViewById(R.id.tv_qy_Admin);
            tvBindUser = itemView.findViewById(R.id.tv_user_bind);
            btnEdit = itemView.findViewById(R.id.btn_edit_seat);
            btnDelete = itemView.findViewById(R.id.btn_delete_seat);
            btnDelete.setVisibility(View.GONE);
        }
    }
}
